package com.snail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.snail.mapper.BizMqMessageMapper;
import com.snail.model.pojo.media.BizMqMessage;
import com.snail.result.Result;
import com.snail.service.BizBigFileService;
import io.minio.MinioClient;
import io.minio.StatObjectArgs;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Monica
 * @description
 * @date 2025/8/22 10:01
 */
@Service
@Slf4j
public class BizBigFileServiceImpl implements BizBigFileService {
    @Autowired
    private BizMqMessageMapper bizMqMessageMapper;
    @Autowired
    private BizFilesServiceImpl bizFilesService;
    @Autowired
    private MinioClient minioClient;
    //图片文件桶
    @Value("${minio.bucket.pic}")
    private String bucketPic;
    @Value("${minio.bucket.video}")
    private String bucketVideo;
    @Value("${minio.bucket.doc}")
    private String bucketDoc;
    @Value("${minio.endpoint}")
    private String bucketPoint;
    @Override
    public Result<List<Integer>> checkChunk(String fileMd5, List<Integer> chunkNums, Long userId) {

        LambdaQueryWrapper<BizMqMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BizMqMessage::getBusinessId,fileMd5);
        boolean flag = bizMqMessageMapper.exists(queryWrapper);
        if (!flag) {
            BizMqMessage bizMqMessage = new BizMqMessage();
            bizMqMessage.setBusinessId(fileMd5);
            bizMqMessageMapper.insert(bizMqMessage);
        }

        String chunkFileFolderPath = bizFilesService.getChunkFileFolderPath(fileMd5);
        // 存储不存在的分块号
        List<Integer> notExistChunkNums = new ArrayList<>();
        for (int i = 0; i < chunkNums.size(); i++) {
            String chunkFilePath = chunkFileFolderPath + chunkNums.get(i);
            InputStream inputStream = null;
            try {
                // 方式1：使用statObject()轻量校验（推荐，仅检查元数据）
                minioClient.statObject(StatObjectArgs.builder().bucket(bucketVideo).object(chunkFilePath).build());
            } catch (Exception e) {
                // 抛异常说明分块不存在，加入列表
                notExistChunkNums.add(chunkNums.get(i));
                log.debug("分块不存在，md5:{}，分块号:{}", fileMd5, i);
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException ex) {
                        log.error("关闭分块输入流失败", ex);
                    }
                }
            }
        }
        return Result.success(notExistChunkNums);
    }

    @Override
    public boolean uploadChunk(String fileMd5, List<Integer> chunkNums, List<String> tempFilePaths) {

        return false;
    }
}





























